<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>AI简历分析助手</title>
    <link rel="stylesheet" href="style.css">
    <link rel="stylesheet" href="wait.css">

<!--    markdown.css 样式文件-->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/styles/github-dark.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/5.1.0/github-markdown-dark.min.css">
</head>
<body>
<!-- 历史对话侧边栏 -->
<div class="history-sidebar" id="historySidebar">
    <div class="sidebar-header">
        <h3>历史对话</h3>
        <button id="toggleSidebar" class="toggle-btn">
            <i class="fas fa-chevron-right"></i>
        </button>
    </div>
    <div class="search-box">
        <input type="text" id="historySearch" placeholder="搜索历史对话...">
        <button id="searchButton"><i class="fas fa-search"></i>搜索</button>
    </div>
    <div class="history-list" id="historyList">
        <!-- 历史对话将通过JS动态填充 -->
    </div>
</div>
<!-- 主聊天区域 -->
<div class="chat-container">
    <!-- 顶部导航栏 -->
    <div class="top-navbar">
        <button id="toggleHistory" class="toggle-btn">
            <i class="fas fa-history"></i> 历史对话
        </button>
        <div class="logo">AI简历分析助手</div>
        <!-- AI服务选择器 -->
        <div class="ai-service-selector">
            <label for="aiService">AI服务:</label>
            <select id="aiService" onchange="setAIService()">
                <option value="deepseek">DeepSeek</option>
                <option value="wenxin">文心一言</option>
            </select>
        </div>
        <div class="user-info">
            <span id="usernameDisplay">用户</span>
            <button id="userManagementBtn" class="action-button">用户管理</button>
            <button id="logoutBtn" class="logout-btn">退出</button>
        </div>
    </div>

    <div class="chat-messages" id="chatMessages">
        <!-- 消息将动态添加到这里 -->
    </div>
    <!-- 在chat-input区域增加文本输入框 -->
    <div class="chat-input">
        <input type="file" id="resumeUpload" accept=".pdf,.doc,.docx">
        <input type="text" id="messageInput" placeholder="输入消息...">
        <button onclick="sendMessage()">发送</button>
        <button onclick="uploadResume()">上传简历</button>
    </div>

</div>
<script src="script.js"></script>
<!--markdown.js 脚本文件-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/marked/4.0.2/marked.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/highlight.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/languages/python.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/languages/java.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/languages/javascript.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/languages/xml.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/languages/css.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/languages/shell.min.js"></script>

<!-- 如果需要数学公式支持 -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.0/es5/tex-mml-chtml.min.js"></script>
<script>
    // 添加退出功能
    document.getElementById('logoutBtn').addEventListener('click', logout);

    // function logout() {
    //     // 清除本地存储的token
    //     localStorage.removeItem('token');
    //     // 清除会话ID
    //     sessionId = null;
    //     // 跳转到登录页
    //     window.location.href = 'login.html';
    //     alert('已退出登录！');
    // }移动到script.js中

    // 页面加载时检查token
    document.addEventListener('DOMContentLoaded', function () {
        const token = localStorage.getItem('token');
        if (!token) {
            window.location.href = 'login.html';
        } else {
            // 解析token获取用户名
            try {
                const payload = JSON.parse(atob(token.split('.')[1]));
                if (payload.username) {
                    document.getElementById('usernameDisplay').textContent = payload.username;
                }
            } catch (e) {
                console.error('解析token失败', e);
            }

            // 绑定用户管理按钮事件
            document.getElementById('userManagementBtn').addEventListener('click', function() {
                window.location.href = 'users.html';
            });

            // 在解析token时添加角色检查
            try {
                const payload = JSON.parse(atob(token.split('.')[1]));
                if (payload.username) {
                    document.getElementById('usernameDisplay').textContent = payload.username;
                }

                // 根据角色显示/隐藏用户管理按钮
                if (payload.roles && (payload.roles.includes('ROLE_ROOT') || payload.roles.includes('ROLE_ADMIN'))) {
                    document.getElementById('userManagementBtn').style.display = 'inline-block';
                } else {
                    document.getElementById('userManagementBtn').style.display = 'none';
                }
            } catch (e) {
                console.error('解析token失败', e);
            }

            // 绑定退出按钮事件
            // document.getElementById('logoutBtn').addEventListener('click', logout);
        }
        // 获取历史对话
        fetchHistory();

        // 绑定侧边栏切换按钮
        document.getElementById('toggleHistory').addEventListener('click', toggleSidebar);
        document.getElementById('toggleSidebar').addEventListener('click', toggleSidebar);

        // 绑定搜索按钮
        document.getElementById('searchButton').addEventListener('click', searchHistory);
        document.getElementById('historySearch').addEventListener('keypress', function(e) {
            if (e.key === 'Enter') {
                searchHistory();
            }
        });
    });
</script>

</body>
</html>